package cn.tianyigps.tianyigpsm7clean.assign;

import cn.tianyigps.tianyigpsm7clean.util.BeanAssginUtil;
import cn.tianyigps.tianyigpsm7clean.util.MapTransformUtils;
import cn.tianyigps.tianyigpspublic.bean.BaseStation;
import cn.tianyigps.tianyigpspublic.bean.EquipmentModel;
import cn.tianyigps.tianyigpspublic.bean.StandardModel;
import cn.tianyigps.tianyigpspublic.rest.RestLocation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created with IDEA
 *
 * @description: 设备赋值
 * @author: cui.guibin
 * @create: 2019/4/18 13:46
 **/
@Component
@Slf4j
public class EquipmentAssign {

    @Autowired
    private RestLocation restLocation;

    public void assgin(EquipmentModel equipmentModel, List<StandardModel> standardModels) {

        Map<String, Object> extend = equipmentModel.getExtend();
        if (!Optional.ofNullable(extend).isPresent())
            extend = new HashMap<>();

        for (StandardModel standardModel :standardModels) {
            
            Map<String, Object> fExtend = standardModel.getExtend();
            String acc = (String) fExtend.get("acc");
            equipmentModel.setAcc(acc);
            extend.putAll(fExtend);
            BeanAssginUtil.copyFieldJavaBean(equipmentModel,standardModel);
        }

        equipmentModel.setExtend(extend);
        equipmentModel.setCleanTime(new Date());
        assginLocation(equipmentModel);
        assginByJudge(equipmentModel);
    }

    private void assginLocation(EquipmentModel equipmentModel) {

        if ("00".equals(equipmentModel.getLocationType())){
            Double gpsLatitude = equipmentModel.getGpsLatitude();
            Double gpsLongitude = equipmentModel.getGpsLongitude();
            if (Optional.ofNullable(gpsLatitude).isPresent() && Optional.ofNullable(gpsLongitude).isPresent()) {
                // 转成国测局坐标（火星坐标）
                Point point = MapTransformUtils.wgs84togcj02(gpsLongitude, gpsLatitude);
                equipmentModel.setGpsLongitude(point.getX());
                equipmentModel.setGpsLatitude(point.getY());
                equipmentModel.setPoint(new GeoJsonPoint(point));
            }
        }else if ("01".equals(equipmentModel.getLocationType())){
            String lac = equipmentModel.getLac();
            String cellId = equipmentModel.getCellId();
            if (!StringUtils.isEmpty(lac) && !StringUtils.isEmpty(cellId)) {
                BaseStation baseStation = restLocation.getBaseStationByRestApi(lac, cellId);
                if (Optional.ofNullable(baseStation).isPresent()){
                    equipmentModel.setGpsLongitude(baseStation.getPoints().getX());
                    equipmentModel.setGpsLatitude(baseStation.getPoints().getY());
                }else{
                    log.error("设备： " + equipmentModel.getEquipmentId() + "   基站解析失败！");
                }
            }
        }
    }

    public void assginByJudge(EquipmentModel equipmentModel) {

        String lineStatus = equipmentModel.getLineStatus();
        Double speed = equipmentModel.getSpeed();
        if (Optional.ofNullable(speed).isPresent() && speed >= 5)
            equipmentModel.setLineStatus("1000");
        else
            equipmentModel.setLineStatus("1002");
        if (!equipmentModel.getLineStatus().equals(lineStatus))
            equipmentModel.setLineStatusTime(new Date());
    }

    public void assginSupplement(List<EquipmentModel> equipmentModels, List<StandardModel> standardModels) {

        for (StandardModel standardModel :standardModels) {

            EquipmentModel model = new EquipmentModel();
            BeanAssginUtil.copyFieldJavaBean(model,standardModel);
            model.setCleanTime(new Date());
            assginLocation(model);

            assginByJudge(model);
            Double gpsLongitude = model.getGpsLongitude();
            Double gpsLatitude = model.getGpsLatitude();

            if (Optional.ofNullable(gpsLongitude).isPresent() && Optional.ofNullable(gpsLatitude).isPresent()){
                if (StringUtils.isEmpty(model.getLocationType()))
                    model.setLocationType("00");
                equipmentModels.add(model);
            }
        }
    }
}
